package com.qianxx.component.activity.effects.shopping_car;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianxx.component.R;
import com.qianxx.component.activity.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShoppingCarActivity extends BaseActivity {

    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.iv_car)
    ImageView ivCar;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.iv_ball)
    ImageView ivBall;

    int[] startLocation = new int[2];
    int[] endLocation = new int[2];

    int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_car);
        ButterKnife.bind(this);
    }

    private void startAnim() {

        count++;
        ivAdd.getLocationOnScreen(startLocation);
        ivCar.getLocationOnScreen(endLocation);

        // 计算位移
        int endX = endLocation[0] - startLocation[0];// 动画位移的X坐标
        int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标
        TranslateAnimation translateAnimationX = new TranslateAnimation(0,
                endX, 0, 0);
        translateAnimationX.setInterpolator(new LinearInterpolator());
        translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
                0, endY);
        translateAnimationY.setInterpolator(new AccelerateInterpolator());
        translateAnimationY.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        RotateAnimation rotateAnimation = new RotateAnimation(0, -360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setInterpolator(new AccelerateInterpolator());
        rotateAnimation.setRepeatCount(0);// 动画重复执行的次数
        rotateAnimation.setFillAfter(true);

        final AnimationSet set = new AnimationSet(false);
        set.setFillAfter(false);
        set.addAnimation(rotateAnimation);
        set.addAnimation(translateAnimationY);
        set.addAnimation(translateAnimationX);
        set.setDuration(500);// 动画的执行时间
        ivBall.startAnimation(set);
        // 动画监听事件
        set.setAnimationListener(new Animation.AnimationListener() {
            // 动画的开始
            @Override
            public void onAnimationStart(Animation animation) {
                ivBall.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            // 动画的结束
            @Override
            public void onAnimationEnd(Animation animation) {
                ivBall.setVisibility(View.GONE);
                tvNumber.setText(count + "");
                set.cancel();
                animation.cancel();
            }
        });

    }


    @Override
    public String setTitle() {
        return "ShoppingCar";
    }

    @OnClick(R.id.iv_add)
    public void onClick() {
        startAnim();
    }
}
